<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>创建情侣关系 - 朋友管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: #f5f7fa;
            color: #333;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px 0;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        
        .header-content {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .logo-section {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .logo {
            width: 40px;
            height: 40px;
            filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.1));
        }
        
        .header h1 {
            font-size: 24px;
            font-weight: 500;
        }
        
        .back-btn {
            background: rgba(255, 255, 255, 0.2);
            color: white;
            border: 1px solid rgba(255, 255, 255, 0.3);
            padding: 8px 16px;
            border-radius: 6px;
            cursor: pointer;
            text-decoration: none;
            transition: all 0.3s ease;
        }
        
        .back-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }
        
        .main-content {
            max-width: 800px;
            margin: 30px auto;
            padding: 0 20px;
        }
        
        .create-form {
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
        }
        
        .form-title {
            text-align: center;
            font-size: 24px;
            font-weight: 600;
            color: #333;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 2px solid #f0f0f0;
        }
        
        .form-row {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
            margin-bottom: 20px;
        }
        
        .form-group {
            display: flex;
            flex-direction: column;
        }
        
        .form-label {
            font-size: 14px;
            font-weight: 500;
            color: #333;
            margin-bottom: 8px;
        }
        
        .form-select {
            padding: 12px 15px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 14px;
            background: white;
            transition: border-color 0.3s ease;
        }
        
        .form-select:focus {
            outline: none;
            border-color: #e91e63;
        }
        
        .form-input {
            padding: 12px 15px;
            border: 2px solid #e1e5e9;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.3s ease;
        }
        
        .form-input:focus {
            outline: none;
            border-color: #e91e63;
        }
        
        .form-actions {
            text-align: center;
            margin-top: 30px;
            padding-top: 20px;
            border-top: 2px solid #f0f0f0;
        }
        
        .btn {
            display: inline-block;
            padding: 12px 30px;
            margin: 0 10px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 14px;
            text-decoration: none;
            transition: all 0.3s ease;
        }
        
        .btn-primary {
            background: #e91e63;
            color: white;
        }
        
        .btn-primary:hover {
            background: #c2185b;
            transform: translateY(-2px);
        }
        
        .btn-secondary {
            background: #95a5a6;
            color: white;
        }
        
        .btn-secondary:hover {
            background: #7f8c8d;
            transform: translateY(-2px);
        }
        
        .loading {
            text-align: center;
            padding: 60px;
            color: #666;
        }
        
        .error {
            text-align: center;
            padding: 60px;
            color: #e74c3c;
        }
        
        .info-box {
            background: #e3f2fd;
            color: #1976d2;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            border-left: 4px solid #2196f3;
        }
        
        .info-box h3 {
            margin-bottom: 10px;
            color: #1976d2;
        }
        
        .info-box p {
            margin-bottom: 8px;
            font-size: 14px;
        }
        
        @media (max-width: 768px) {
            .form-row {
                grid-template-columns: 1fr;
            }
            
            .form-actions {
                display: flex;
                flex-direction: column;
                gap: 10px;
            }
            
            .btn {
                margin: 0;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="header-content">
            <div class="logo-section">
                <img src="/static/images/logo.svg" alt="朋友管理系统" class="logo">
                <h1>创建情侣关系</h1>
            </div>
            <a href="/couple/list" class="back-btn">返回情侣列表</a>
        </div>
    </div>
    
    <div class="main-content">
        <div class="info-box">
            <h3>💕 创建情侣关系说明</h3>
            <p>• 只能选择两个朋友创建情侣关系</p>
            <p>• 每个朋友只能同时存在于一个情侣关系中</p>
            <p>• 在一起的天数会在每天零点自动更新</p>
            <p>• 可以随时结束或删除情侣关系</p>
        </div>
        
        <div id="createFormContainer">
            <div class="loading">加载中...</div>
        </div>
    </div>
    
    <script>
        // 页面加载完成后获取朋友列表
        document.addEventListener('DOMContentLoaded', function() {
            loadFriends();
        });
        
        // 加载朋友列表
        function loadFriends() {
            fetch('/friend/api/list')
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        displayCreateForm(data.data);
                    } else {
                        showError(data.message);
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    showError('加载朋友列表失败');
                });
        }
        
        // 显示创建表单
        function displayCreateForm(friends) {
            const container = document.getElementById('createFormContainer');
            
            if (!friends || friends.length === 0) {
                showError('没有可用的朋友信息');
                return;
            }
            
            // 过滤掉已经在情侣关系中的朋友
            const availableFriends = friends.filter(friend => !friend.inCouple);
            
            if (availableFriends.length < 2) {
                showError('需要至少两个可用的朋友来创建情侣关系');
                return;
            }
            
            const today = new Date().toISOString().split('T')[0];
            
            container.innerHTML = `
                <form class="create-form" onsubmit="createCouple(event)">
                    <div class="form-title">💕 创建新的情侣关系</div>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">选择第一个朋友 *</label>
                            <select class="form-select" id="friend1Id" name="friend1Id" required>
                                <option value="">请选择朋友</option>
                                ${availableFriends.map(friend => `
                                    <option value="${friend.id}">${friend.name || '未知姓名'}</option>
                                `).join('')}
                            </select>
                        </div>
                        
                        <div class="form-group">
                            <label class="form-label">选择第二个朋友 *</label>
                            <select class="form-select" id="friend2Id" name="friend2Id" required>
                                <option value="">请选择朋友</option>
                                ${availableFriends.map(friend => `
                                    <option value="${friend.id}">${friend.name || '未知姓名'}</option>
                                `).join('')}
                            </select>
                        </div>
                    </div>
                    
                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">开始在一起的日期 *</label>
                            <input type="date" class="form-input" id="startDate" name="startDate" value="${today}" required>
                        </div>
                    </div>
                    
                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">💕 创建情侣关系</button>
                        <a href="/couple/list" class="btn btn-secondary">取消</a>
                    </div>
                </form>
            `;
            
            // 添加选择验证
            document.getElementById('friend1Id').addEventListener('change', validateFriendSelection);
            document.getElementById('friend2Id').addEventListener('change', validateFriendSelection);
        }
        
        // 验证朋友选择
        function validateFriendSelection() {
            const friend1Id = document.getElementById('friend1Id').value;
            const friend2Id = document.getElementById('friend2Id').value;
            
            if (friend1Id && friend2Id && friend1Id === friend2Id) {
                alert('不能选择同一个朋友');
                document.getElementById('friend2Id').value = '';
            }
        }
        
        // 创建情侣关系
        function createCouple(event) {
            event.preventDefault();
            
            const friend1Id = document.getElementById('friend1Id').value;
            const friend2Id = document.getElementById('friend2Id').value;
            const startDate = document.getElementById('startDate').value;
            
            if (!friend1Id || !friend2Id || !startDate) {
                alert('请填写所有必填字段');
                return;
            }
            
            if (friend1Id === friend2Id) {
                alert('不能选择同一个朋友');
                return;
            }
            
            const formData = {
                friend1Id: friend1Id,
                friend2Id: friend2Id,
                startDate: startDate
            };
            
            fetch('/couple/create', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(formData)
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert(data.message);
                    window.location.href = '/couple/list';
                } else {
                    alert('创建失败：' + data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('创建失败，请稍后重试');
            });
        }
        
        // 显示错误信息
        function showError(message) {
            const container = document.getElementById('createFormContainer');
            container.innerHTML = `
                <div class="error">
                    <h3>出错了</h3>
                    <p>${message}</p>
                    <a href="/couple/list" class="btn btn-primary">返回情侣列表</a>
                </div>
            `;
        }
    </script>
</body>
</html>
